<template>
    <div>
        <cfg-table :columns="columns" :data="data" :loading="loading" :config="config"
                   :pageSizeOpts="pageSizeOpts" :pageNavigation="pageNavigation"
                   :queryFormItem="queryFormItem" :collapseFormItem="collapseFormItem"
                   @loadPage="loadPage"
                   @handleCreate="handleCreate"
                   @handleDetail="handleDetail"
                   @handleEdit="handleEdit"
                   @handleDelete="handleDelete"
                   @handleDeleteMultiple="handleDeleteMultiple"
                   @handleSearch="handleSearch"
                   :additionalAction="additionalAction"
        ></cfg-table>
        <model-editor v-if="drawer.show" :data="record" :model-type="'template'" :drawer="drawer"
                      @dataSubmit="handleDataSubmit"
        ></model-editor>
    </div>

</template>

<script>
import ModelEditor from "../parts/cfg-model-editor";
    export default {
        name: 'statisticsVersion',
        components: {ModelEditor},
        data () {
            return {
                classObject: null,
                record: {},
                collectionObject: null,
                loading: false,
                drawer: {
                    show: false,
                    type: 'read'
                },
                config: {
                    search: true,
                    addButton: false,
                    batchDeleteButton: true,
                    downloadIcon: true,
                    uploadIcon: true,
                    uploadUrl: '/',
                    tableSizeIcon: true,
                    fullscreenIcon: true,
                    refreshIcon: true,
                    columnSettingIcon: true,
                    action: false,
                    additionalAction: true
                },
                pageSizeOpts: [10, 30, 50, 100],
                pageNavigation: { total: 0, pageSize: 10, pageNumber: 1 }
            }
        },
        props: {},
        computed: {
            columns () {
                return [
                    {
                        key: 'type',
                        title: {
                            'zh-CN': '所属类型',
                            'en-US': 'type'
                        }[this.G_Locale],
                        minWidth: 100,
                        align: 'center',
                        show: true
                    },
                    {
                        key: 'className',
                        title: {
                            'zh-CN': '模型类名',
                            'en-US': 'class name'
                        }[this.G_Locale],
                        minWidth: 100,
                        align: 'center',
                        show: true
                    },
                    {
                        key: 'version',
                        title: {
                            'zh-CN': '版本号',
                            'en-US': 'version'
                        }[this.G_Locale],
                        minWidth: 100,
                        align: 'center',
                        show: true,
                    },
                    {
                        key: 'published',
                        title: {
                            'zh-CN': '是否发布',
                            'en-US': 'published'
                        }[this.G_Locale],
                        minWidth: 100,
                        align: 'center',
                        show: true
                    },
                    {
                        key: 'createTime',
                        title: {
                            'zh-CN': '创建时间',
                            'en-US': 'create time'
                        }[this.G_Locale],
                        minWidth: 60,
                        align: 'center',
                        show: true,
                    },
                    {
                        key: 'updateTime',
                        title: {
                            'zh-CN': '更新时间',
                            'en-US': 'update time'
                        }[this.G_Locale],
                        minWidth: 100,
                        align: 'center',
                        show: true,
                    },
                ]
            },
            queryFormItem () {
                return [
                    {
                        key: 'type',
                        title: {
                            'zh-CN': '所属类型',
                            'en-US': 'type'
                        }[this.G_Locale],
                        config: {
                            type: 'text'
                        }
                    },
                    {
                        key: 'className',
                        title: {
                            'zh-CN': '模型类名',
                            'en-US': 'class name'
                        }[this.G_Locale],
                        config: {
                            type: 'text'
                        }
                    },
                ]
            },
            collapseFormItem () {
                return [
                    {
                        key: 'type',
                        title: {
                            'zh-CN': '所属类型',
                            'en-US': 'type'
                        }[this.G_Locale],
                        config: {
                            type: 'text'
                        }
                    },
                    {
                        key: 'className',
                        title: {
                            'zh-CN': '模型类名',
                            'en-US': 'class name'
                        }[this.G_Locale],
                        config: {
                            type: 'text'
                        }
                    },
                ]
            },
            additionalAction () {
                return {
                    key: 'additionalAction',
                    title: {
                        'zh-CN': '操作',
                        'en-US': 'action'
                    }[this.G_Locale],
                    slot: 'additionalAction',
                    width: 280,
                    fixed: 'right',
                    align: 'center',
                    show: true,
                    buttons: [
                        {
                            title: {
                                'zh-CN': '发布/取消发布',
                                'en-US': 'published/unpublished'
                            }[this.G_Locale], color: 'green', event: (row, index) => this.handlePublished(row)
                        },
                        {
                            title: {
                                'zh-CN': '升级',
                                'en-US': 'upgrade'
                            }[this.G_Locale], color: 'gold', event: (row, index) => this.handleUpgrade(row,index)
                        },
                        {
                            title: {
                                'zh-CN': '删除',
                                'en-US': 'delete'
                            }[this.G_Locale], color: 'red', event: (row, index) => this.handleDelete(row,index)
                        },
                        {
                            title: {
                                'zh-CN': '查看',
                                'en-US': 'view'
                            }[this.G_Locale], color: 'blue', event: (row, index) => this.handleDetail(row,index)
                        },
                    ]
                }
            },
            data () {
                if (!!this.collectionObject) {
                    return this.collectionObject.records;
                } else {
                    return [];
                }
            }
        },
        beforeMount () {
            this.collectionObject = this.$modelObject.create('statisticsModelCollection');
            this.classObject = this.$modelObject.create('statisticsModel')
            this.loadPage(this.pageNavigation, null)
        },
        mounted () {

        },
        methods: {
            loadPage (pageNavigation, filter) {
                let newFilter = '1=1';
                if (!!filter && Object.keys(filter).length > 0) {
                    newFilter = newFilter + '&&' + this.collectionObject.util.getFilter(filter);
                }
                this.loading = true;
                this.collectionObject.queryIncludeUnpublishedByPage(pageNavigation.pageNumber, pageNavigation.pageSize, newFilter).then(flag => {
                    if (flag) {
                        this.pageNavigation.total = this.collectionObject.total;
                        this.loading = false;
                    } else {
                        this.loading = false;
                    }
                })
            },
            handleSearch (filter) {
                this.pageNavigation.pageNumber = 1;
                this.loadPage(this.pageNavigation, filter)
            },
            handleCreate () {
                this.record = this.$modelObject.getTpl('templateModel');
                this.$set(this.drawer, 'show', true);
                this.$set(this.drawer, 'type', 'new');
            },
            handleDetail (row, index) {
                this.record = row;
                this.$set(this.drawer, 'show', true);
                this.$set(this.drawer, 'type', 'read');
            },
            handleEdit (row, index) {
                this.record = row;
                this.$set(this.drawer, 'show', true);
                this.$set(this.drawer, 'type', 'edit');
            },
            handleDelete (row, index) {
                this.deleteRecord([row.id])
            },
            handleDeleteMultiple (selection) {
                let ids = [];
                selection.map(item => ids.push(item.id));
                this.deleteRecord(ids)
            },
            deleteRecord (ids) {
                let _this = this;
                this.loading = true;
                this.collectionObject.deleteByIds(ids).then(flag => {
                    if (flag) {
                        ids.forEach(id => {
                            let index = _this.collectionObject.records.findIndex(item => item.id === id);
                            if (index !== -1) {
                                _this.collectionObject.records.splice(index, 1)
                            }
                        })
                        this.loading = false;
                    }
                })
            },
            handleDataSubmit (data, type) {
                console.log(data)
                if (type === 'new') {
                    this.loading = true;
                    this.classObject.add(data).then(flag => {
                        if (flag) {
                            this.collectionObject.records.unshift(this.classObject.record);
                            this.loading = false;
                            this.$set(this.drawer, 'show', false);
                        }
                    })
                } else if (type === 'edit') {
                    this.loading = true;
                    this.classObject.setRecord(data);
                    this.classObject.update().then(flag => {
                        if (flag) {
                            let index = this.collectionObject.records.findIndex(item => item.id === this.classObject.record.id);
                            if (index !== -1) {
                                this.collectionObject.records.splice(index, 1, this.classObject.record)
                            }
                            this.loading = false;
                            this.$set(this.drawer, 'show', false);
                        }
                    })
                }
            },
            handleUpgrade (row) {
                this.loading = true;
                let className = row.className;
                let version =row.version;
                let published = row.published;
                if(published){
                    this.classObject.upgrade(className,version).then(flag => {
                        if (flag) {
                            this.loadPage(this.pageNavigation, null)
                            this.loading = false;
                        }
                    })
                }
            },
            handlePublished (row) {
                this.loading = true;
                let className = row.className;
                let version =row.version;
                let published = row.published;
                if(published){
                    this.classObject.unpublished(className,version).then(flag => {
                        if (flag) {
                            this.loadPage(this.pageNavigation, null)
                            this.loading = false;
                        }
                    })
                }else{
                    this.classObject.published(className,version).then(flag => {
                        if (flag) {
                            this.loadPage(this.pageNavigation, null)
                            this.loading = false;
                        }
                    })
                }
            }
        }
    }
</script>

<style scoped>

</style>
